package cn.innosmart.aq.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.ReminderListAdapter;
import cn.innosmart.aq.bean.Reminder;
import cn.innosmart.aq.reminder.ReminderManager;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2016/1/12.
 */
public class ReminderListActivity extends BaseActivity {
    private final int MODIFY = 1;
    private ListView lvReminder;
    private TextView tvEmpty;
    private Toolbar toolbar;
    private Menu menus;
    private boolean isModify;
    private ArrayList<Reminder> reminders = new ArrayList<>();
    private ReminderListAdapter reminderListAdapter;
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_add:
                    Intent intent = new Intent(ReminderListActivity.this, ReminderActivity.class);
                    intent.putExtra("flag", 0);
                    startActivityForResult(intent, 2);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//                    FragmentManagerTool.addToBackStackFragment(getActivity().getSupportFragmentManager(), new ReminderFragment(), R.id.boxdetail_content);
                    break;
                case R.id.action_edit:
                    isModify = !isModify;
                    if (isModify) {
                        menus.findItem(R.id.action_add).setVisible(false);
                        menus.findItem(R.id.action_edit).setTitle(R.string.action_exit_edit);
                    } else {
                        menus.findItem(R.id.action_add).setVisible(true);
                        menus.findItem(R.id.action_edit).setTitle(R.string.edit);
                    }
                    reminderListAdapter.startModify(isModify);
                    break;
            }
            return true;
        }
    };
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Reminder reminder = (Reminder) buttonView.getTag();
            if (isChecked) {
                reminder.isEnable = true;
                ReminderManager.createAlarm(ReminderListActivity.this, reminder);
            } else {
                reminder.isEnable = false;
                ReminderManager.cancelAlarm(ReminderListActivity.this, reminder);
            }
            SharedUtil.getInstance().updateReminder(reminder);
        }
    };
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Reminder reminder = (Reminder) v.getTag();
            switch (v.getId()) {
                case R.id.tv_delete:
                    if (reminder.isEnable = true) {
                        ReminderManager.cancelAlarm(ReminderListActivity.this, reminder);
                    }
                    SharedUtil.getInstance().removeReminder(reminder.id);
                    reminders.remove(reminder);
                    reminderListAdapter.refresh(reminders);
                    break;
                case R.id.rl_bg:
                    Intent intent = new Intent(ReminderListActivity.this, ReminderActivity.class);
                    intent.putExtra("flag", 1);
                    intent.putExtra("reminder", reminder);
                    startActivityForResult(intent, 1);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                    break;
            }
        }
    };
    private View.OnClickListener onNavigationClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            exit();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder_list);
        setBar();
        getData();
        lvReminder = (ListView) findViewById(R.id.lv_reminder);
        reminderListAdapter = new ReminderListAdapter(this, reminders);
        tvEmpty = (TextView) findViewById(R.id.tv_empty);
        lvReminder.setEmptyView(tvEmpty);
        reminderListAdapter.setOnClickListener(onClickListener);
        reminderListAdapter.setOnCheckedChangeListener(onCheckedChangeListener);
        lvReminder.setAdapter(reminderListAdapter);
    }

    private void getData() {
        reminders = SharedUtil.getInstance().queryAllReminder();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
//        toolbar.setTitle(getString(R.string.activity_more_reminder));
        TextView tv = (TextView) toolbar.findViewById(R.id.tv_toolbar);
        tv.setText(getString(R.string.activity_more_reminder));
        setSupportActionBar(toolbar);
//        toolbar.setNavigationOnClickListener(onNavigationClickListener);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_assign, menu);
        menus = menu;
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == MODIFY) {

        } else if (requestCode == 2 && resultCode == 2) {

        }
        reminders = SharedUtil.getInstance().queryAllReminder();
        reminderListAdapter.refresh(reminders);
    }
}
